<script setup lang="ts">
import {onMounted, ref} from "vue";
import {reqCancelSale, reqDeleteSku, reqSaleSku, reqSkuList} from "@/api/product/sku";
import {SkuData, SkuResponseData} from "@/api/product/sku/type.ts";
import {ElMessage} from "element-plus";
onMounted(()=> {
  getHasSku()
})

const pageNum = ref<number>(1)
const pageSize = ref<number>(10)
const total = ref<number>(0)
const skuArr = ref<Array<SkuData>>([])
const drawer = ref(false)

const getHasSku = async (pager=1) => {
  pageNum.value = pager
  const result:SkuResponseData = await reqSkuList(pageNum.value, pageSize.value)
  if(result.code === 200) {
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}

const sizeChange = () => {
  getHasSku()
}

const updateSale = async (row:SkuData) => {
  if(row.isSale === 1) {
    await reqCancelSale(row.id as number)
    ElMessage({
      type:'success',
      message:'下架成功'
    })
    await getHasSku(pageNum.value)
  }else {
    await reqSaleSku(row.id as number)
    ElMessage({
      type:'success',
      message:'上架成功'
    })
    await getHasSku(pageNum.value)
  }
}

const deleteSku = async (row:SkuData) => {
  const result = await reqDeleteSku(row.id as number)
  if(result.code === 200) {
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    await getHasSku(skuArr.value.length>1?pageNum.value:pageNum.value-1)
  }else {
    ElMessage({
      type:'error',
      message:'删除失败'
    })
  }
}
</script>

<template>
  <el-card>
    <el-table border :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column show-overflow-tooltip width="250px" label="名称" prop="skuName"></el-table-column>
      <el-table-column show-overflow-tooltip width="250px" label="描述" prop="skuDesc"></el-table-column>
      <el-table-column show-overflow-tooltip width="250px" label="图片">
        <template v-slot="{row}">
          <img :src="row.skuDefaultImg" style="width: 100px;height: 100%" alt="">
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="250px" label="重量" prop="weight"></el-table-column>
      <el-table-column show-overflow-tooltip width="250px" label="价格" prop="price"></el-table-column>
      <el-table-column show-overflow-tooltip width="250px" fixed="right" label="操作">
        <template v-slot="{row}">
          <el-button @click="updateSale(row)" type="warning" size="default" :icon="row.isSale===1?'Bottom':'Top'"></el-button>
          <el-button type="success" size="default" icon="Edit"></el-button>
          <el-button type="info" size="default" icon="InfoFilled"></el-button>
          <el-button type="danger" @click="deleteSku(row)" size="default" icon="Delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        style="margin-top: 20px"
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getHasSku"
        @size-change="sizeChange"
    />
    <el-drawer v-model="drawer" :direction="'rtl'">
      <template v-slot:header>
        <h4>查看商品详情</h4>
      </template>
    </el-drawer>
  </el-card>
</template>

<style scoped lang="scss">

</style>